<template>
  <div v-loading="loading" class="block">
    <div class="header">
      <div class="flex">
        <div class="icon" />
        <div class="title">区块</div>
      </div>
      <div class="more" @click="more()">更多</div>
    </div>
    <div class="body">
      <div v-for="(item, index) in dataList" :key="index" class="block_item">
        <div class="left">
          <div class="height">块高 {{ item.blockNumber }}</div>
          <div class="time">{{ item.blockTimestamp }}</div>
        </div>
        <div class="right">
          <div class="author textOverflow">出块者 {{ item.sealer }}</div>
          <div class="amount">{{ item.transCount }} txns</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getblockList } from '@/api/overview'
export default {
  name: 'Block',
  components: { },
  props: {
    canShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dataList: [],
      loading: true
    }
  },
  watch: {
    canShow(n, o) {
      if (n) {
        this.loadData()
      }
    }
  },
  mounted() {
    if (this.canShow) {
      this.loadData()
    }
  },
  methods: {
    async loadData() {
      try {
        this.loading = true
        const param = {
          groupId: 1,
          pageNumber: 1,
          pageSize: 3
        }
        const { code, data } = await getblockList(param)
        if (!code) {
          this.dataList = data
        }
      } catch (error) {
        console.error(error)
      } finally {
        this.loading = false
      }
    },
    more() {
      if (this.canShow) {
        this.$router.push({ path: '/block-info' })
      } else {
        this.$messageOnce.error('无权查看')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.block {
	width: 645px;
	height: 324px;
	margin: 15px;
	padding: 0 20px;
	background: #FFFFFF;
	box-shadow: 0px 6px 25px 0px rgba(0, 0, 0, 0.04);
	border-radius: 10px;
	display: inline-block;
  vertical-align: top;
	.header {
		height: 64px;
		display: flex;
    align-items: center;
    justify-content: space-between;
    .title {
      font-size: 18px;
      font-family: SourceHanSansCN-Medium, SourceHanSansCN;
      font-weight: 500;
      color: #2C2C2C;
    }
    .icon {
      width: 24px;
      height: 24px;
      background: url('../../assets/overview/block.png');
      background-size: 100%;
      margin-right: 20px;
    }
    .more {
      font-size: 14px;
      font-family: SourceHanSansCN-Medium, SourceHanSansCN;
      font-weight: 500;
      color: #666666;
      cursor: pointer;
      margin-right: 10px;
    }
	}
  .body {
    padding: 0 10px;
    .block_item {
      height: 75px;
      padding: 20px 0;
      display: flex;
      justify-content: space-between;
      border-bottom: 1px solid #F2F2F2;
      .left {
        .height{
          height: 14px;
          font-size: 14px;
          font-family: SourceHanSansCN-Medium, SourceHanSansCN;
          font-weight: 500;
          color: $MainColor;
          line-height: 14px;
        }
        .time {
          height: 14px;
          font-size: 14px;
          font-family: SourceHanSansCN-Normal, SourceHanSansCN;
          font-weight: 400;
          color: #999999;
          line-height: 14px;
          margin-top: 8px;
        }
      }
      .right {
        .author {
          width: 110px;
          height: 14px;
          font-size: 14px;
          font-family: SourceHanSansCN-Medium, SourceHanSansCN;
          font-weight: 500;
          color: #434343;
          line-height: 14px;
        }
        .amount {
          text-align: center;
          width: 50px;
          height: 18px;
          background: #F6F6F6;
          font-size: 12px;
          font-family: SourceHanSansCN-Medium, SourceHanSansCN;
          font-weight: 500;
          color: #666666;
          line-height: 18px;
          margin-top: 4px;
          float: right;
        }
      }
    }
  }
}

.textOverflow {
  overflow:hidden;
  text-overflow:ellipsis;
  white-space:nowrap;
}

.flex {
  display: flex;
  align-items: center;
}
</style>
